﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InfoStuffStore.Common;

namespace InfoStuffStore
{
    public partial class FromGetCheckout : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            com.paypal.sdk.services.NVPCallerServices caller = PayPalAPI.PayPalAPIInitialize();
            com.paypal.sdk.util.NVPCodec encoder = new com.paypal.sdk.util.NVPCodec();
            encoder["METHOD"] = "GetExpressCheckoutDetails";
            encoder["TOKEN"] = Session["TOKEN"].ToString();

            string pStrrequestforNvp = encoder.Encode();
            string pStresponsenvp = caller.Call(pStrrequestforNvp);

            com.paypal.sdk.util.NVPCodec decoder = new com.paypal.sdk.util.NVPCodec();
            decoder.Decode(pStresponsenvp);


            string strAck = decoder["ACK"];
            if (strAck != null && (strAck == "Success" || strAck == "SuccessWithWarning"))
            {
                encoder["METHOD"] = "DoExpressCheckoutPayment";
                encoder["TOKEN"] = Session["TOKEN"].ToString();
                encoder["PAYERID"] = decoder["PAYERID"].ToString();
                encoder["AMT"] = decoder["AMT"].ToString();
                encoder["PAYMENTACTION"] = "Sale"; //Request.QueryString["paymentType"];
                encoder["CURRENCYCODE"] = Request.QueryString["currency"];

                pStrrequestforNvp = encoder.Encode();
                pStresponsenvp = caller.Call(pStrrequestforNvp);

                decoder = new com.paypal.sdk.util.NVPCodec();
                decoder.Decode(pStresponsenvp);


                strAck = decoder["ACK"];
                if (strAck != null && (strAck == "Success" || strAck == "SuccessWithWarning"))
                {
                    string trandId = Request.QueryString["tid"];
                    using (DBDataContext dataContext = new DBDataContext())
                    {
                        List<AdSpace> adsSpaceList = dataContext.AdSpaces.Where(obj => (obj.TransCode == trandId)).ToList();
                        if (adsSpaceList.Count > 0)
                        {
                            adsSpaceList[0].IsActive = true;
                            if (adsSpaceList[0].SelectPlan == "20")
                            {
                                adsSpaceList[0].ExpiryaDate = DateTime.Now.AddMonths(1);
                            }
                            else if (adsSpaceList[0].SelectPlan == "45")
                            {
                                adsSpaceList[0].ExpiryaDate = DateTime.Now.AddMonths(3);
                            }
                            else if (adsSpaceList[0].SelectPlan == "75")
                            {
                                adsSpaceList[0].ExpiryaDate = DateTime.Now.AddMonths(6);
                            }
                            else if (adsSpaceList[0].SelectPlan == "99")
                            {
                                adsSpaceList[0].ExpiryaDate = DateTime.Now.AddMonths(12);
                            }
                        }

                        dataContext.SubmitChanges();
                    }

                    Session["result"] = decoder;
                    string pStrResQue = "API=" + "DoExpressCheckoutPayment";
                    Response.Redirect("DoExpressCheckoutPayment.aspx?" + pStrResQue);
                }
                else
                {
                    Session["errorresult"] = decoder;
                    string pStrResQue = "API=" + "Error Detail ";
                    Response.Redirect("APIError.aspx?" + pStrResQue);
                }
            }
            else
            {
                Session["errorresult"] = decoder;
                string pStrResQue = "API=" + "Error Detail ";
                Response.Redirect("APIError.aspx?" + pStrResQue);
            }
        }
    }
}